package com.hangman;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class GameActivity extends Activity {
	private Category chosenCategory;
	private Hangman gameInstance;
	private LinearLayout letterLayout;
	private ArrayList<EditText> letterDisplayers = new ArrayList<EditText>();
	private TextView messageOutput, winsOutput, lossesOutput, triesLeftOutput,
			wrongLetters;
	private boolean gameOver, newWord = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game_layout);

		chosenCategory = (Category) getIntent()
				.getSerializableExtra("category");

		gameInstance = new Hangman(chosenCategory, 10);

		letterLayout = (LinearLayout) findViewById(R.id.letterLayout);
		Button makeGuessButton = (Button) findViewById(R.id.makeGuessButton);
		messageOutput = (TextView) findViewById(R.id.messageOutput);
		winsOutput = (TextView) findViewById(R.id.winsOutput);
		lossesOutput = (TextView) findViewById(R.id.lossesOutput);
		triesLeftOutput = (TextView) findViewById(R.id.triesLeftOutput);
		wrongLetters = (TextView) findViewById(R.id.wrongLetters);

		triesLeftOutput.setText("Tries: " + gameInstance.getTriesLeft());
		winsOutput.setText("Wins: " + gameInstance.getWins());
		lossesOutput.setText("Losses: " + gameInstance.getLosses());

		Log.i("Word: ", gameInstance.getCurrentWord());

		createLetterLines();

		makeGuessButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				if (gameOver) {
					goToMenu();
				}

				EditText guessField = (EditText) findViewById(R.id.inputField);
				try {
					String input = guessField.getText().toString();

					if (newWord) {
						Log.i("Word:", gameInstance.getCurrentWord());
						guessField.setVisibility(View.VISIBLE);
						letterDisplayers.clear();
						letterLayout.removeAllViews();
						createLetterLines();
						triesLeftOutput.setText("Tries: "
								+ gameInstance.getTriesLeft() + "");
						messageOutput.setText("");
						wrongLetters.setText("");
						newWord = false;
						updateGallow();

					}

					if (input != "" && gameInstance.getTriesLeft() > 0) {
						gameInstance.makeGuess(input.charAt(0));
						triesLeftOutput.setText("Tries: "
								+ gameInstance.getTriesLeft() + "");
						wrongLetters.setText(gameInstance.getWrongLetters()
								.toString());
						guessField.setText("");
						updateLetterLines();
						updateGallow();
						InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
						imm.hideSoftInputFromWindow(
								guessField.getWindowToken(), 0);

						if (gameInstance.checkIfWon()) {
							messageOutput.setText("Victory");
							guessField.setVisibility(View.INVISIBLE);
							guessField.setText("");
							newWord = true;
						}
						if (gameInstance.checkIfLost()) {
							messageOutput.setText("Failure");
							guessField.setVisibility(View.INVISIBLE);
							guessField.setText("");
							newWord = true;
						}
					}
				} catch (StringIndexOutOfBoundsException ex) {
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(guessField.getWindowToken(), 0);
				}

				winsOutput.setText("Wins: " + gameInstance.getWins());
				lossesOutput.setText("Losses: " + gameInstance.getLosses());

				if (gameInstance.checkIfFinished()) {
					goToFinished();
				}
			}

		});
	}

	private void updateGallow() {
		ImageView gallowImage = (ImageView) findViewById(R.id.gallowImage);
		switch (gameInstance.getTriesLeft()) {
		case 0:
			gallowImage.setImageResource(R.drawable.gallow_0);
			break;

		case 1:
			gallowImage.setImageResource(R.drawable.gallow_1);
			break;

		case 2:
			gallowImage.setImageResource(R.drawable.gallow_2);
			break;

		case 3:
			gallowImage.setImageResource(R.drawable.gallow_3);
			break;

		case 4:
			gallowImage.setImageResource(R.drawable.gallow_4);
			break;

		case 5:
			gallowImage.setImageResource(R.drawable.gallow_5);
			break;

		case 6:
			gallowImage.setImageResource(R.drawable.gallow_6);
			break;

		case 7:
			gallowImage.setImageResource(R.drawable.gallow_7);
			break;

		case 8:
			gallowImage.setImageResource(R.drawable.gallow_8);
			break;

		case 9:
			gallowImage.setImageResource(R.drawable.gallow_9);
			break;

		case 10:
			gallowImage.setImageResource(R.drawable.gallow_10);
			break;
		}
	}

	// gameInstance.getShowingIndexes().length
	private void createLetterLines() {
		for (int i = 0; i < gameInstance.getShowingIndexes().length; i++) {
			LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			EditText editText = new EditText(this);
			editText.setLayoutParams(lparams);
			editText.setFocusable(false);
			editText.setClickable(false);
			letterDisplayers.add(editText);
			letterLayout.addView(editText);
		}
	}

	private void updateLetterLines() {
		for (int i = 0; i < gameInstance.getShowingIndexes().length; i++) {
			if (gameInstance.getShowingIndexes()[i] == 1) {
				letterDisplayers.get(i).setText(
						gameInstance.getCurrentTask().getCharAt(i) + "");
			}
		}
	}

	public boolean onCreateOptionsMenu(Menu meny) {
		super.onCreateOptionsMenu(meny);
		meny.add("Exit");
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getTitle().equals("Exit")) {
			Intent intent = new Intent(getApplicationContext(),
					LanguagePicker.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.putExtra("doExit", true);
			startActivity(intent);
		}
		return true;
	}

	private void goToMenu() {
		Intent intent = new Intent(getApplicationContext(),
				LanguagePicker.class);
		startActivity(intent);
	}

	private void goToFinished() {
		Intent intent = new Intent("com.hangman.FINISHEDACTIVITY");
		intent.putExtra("result", "W: " + gameInstance.getWins() + "  L: " + gameInstance.getLosses());
		startActivity(intent);
		finish();
	}

}
